package com.ruoyi.teacher.service.impl;

import com.ruoyi.common.constant.StudentConstants;
import com.ruoyi.common.core.text.Convert;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.bean.BeanValidators;
import com.ruoyi.teacher.domain.SysStudent;
import com.ruoyi.teacher.mapper.SysStudentMapper;
import com.ruoyi.teacher.service.ISysStudentService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.validation.Validator;
import java.util.List;

/**
 * sysStudentService业务层处理
 * 
 * @author ruoyi
 * @date 2022-01-13
 */
@Service
public class SysStudentServiceImpl implements ISysStudentService
{
    private static final Logger log = LoggerFactory.getLogger(SysStudentServiceImpl.class);
    @Autowired
    protected Validator validator;
    @Autowired
    private SysStudentMapper sysStudentMapper;

    /**
     * 查询sysStudent
     * 
     * @param id sysStudent主键
     * @return sysStudent
     */
    @Override
    public SysStudent selectSysStudentById(Long id)
    {
        return sysStudentMapper.selectSysStudentById(id);
    }

    /**
     * 查询sysStudent列表
     * 
     * @param sysStudent sysStudent
     * @return sysStudent
     */
    @Override
    public List<SysStudent> selectSysStudentList(SysStudent sysStudent)
    {
        return sysStudentMapper.selectSysStudentList(sysStudent);
    }

    /**
     * 新增sysStudent
     * 
     * @param sysStudent sysStudent
     * @return 结果
     */
    @Override
    public int insertSysStudent(SysStudent sysStudent)
    {
        return sysStudentMapper.insertSysStudent(sysStudent);
    }

    /**
     * 修改sysStudent
     * 
     * @param sysStudent sysStudent
     * @return 结果
     */
    @Override
    public int updateSysStudent(SysStudent sysStudent)
    {
        return sysStudentMapper.updateSysStudent(sysStudent);
    }

    /**
     * 批量删除sysStudent
     * 
     * @param ids 需要删除的sysStudent主键
     * @return 结果
     */
    @Override
    public int deleteSysStudentByIds(String ids)
    {
        return sysStudentMapper.deleteSysStudentByIds(Convert.toStrArray(ids));
    }

    /**
     * 删除sysStudent信息
     * 
     * @param id sysStudent主键
     * @return 结果
     */
    @Override
    public int deleteSysStudentById(Long id)
    {
        return sysStudentMapper.deleteSysStudentById(id);
    }

    /**
     * 校验身份证号 是否存在
     * @param card
     * @return
     */
    @Override
    public String checkCardUnique(String card) {
        int count = sysStudentMapper.checkCardUnique(card);
        if (count > 0)
        {
            return StudentConstants.CARD_NOT_UNIQUE;
        }
        return StudentConstants.CARD_UNIQUE;
    }

    /**
     * 校验身份证号 是否存在
     * @param card
     * @return
     */
    @Override
    public String checkStudentIdUnique(String StudentId) {
        int count = sysStudentMapper.checkStudentIdUnique(StudentId);
        if (count > 0)
        {
            return StudentConstants.STUDENT_ID_NOT_UNIQUE;
        }
        return StudentConstants.STUDENT_ID_UNIQUE;
    }


    @Override
    public String importStudent(List<SysStudent> studentList, Boolean isUpdateSupport, String operName)
    {
        int successNum = 0;
        int failureNum = 0;
        StringBuilder successMsg = new StringBuilder();
        StringBuilder failureMsg = new StringBuilder();
        if (StringUtils.isNull(studentList) || studentList.size() == 0)
        {
            throw new ServiceException("导入学生信息不能为空！");
        }
        for(SysStudent student:studentList){
            try{
                //验证是否存在这个学生
               SysStudent sysStudent = sysStudentMapper.selectSysStudentByUserName(student.getUserName());
               if(StringUtils.isNull(sysStudent)){
                   BeanValidators.validateWithException(validator,student);
                   student.setCreateBy(operName);
                   this.insertSysStudent(student);
                   successNum++;
                   successMsg.append("<br/>" + successNum + "、账号 " + student.getUserName() + " 导入成功");
               }else if (isUpdateSupport){
                   BeanValidators.validateWithException(validator, student);
                   student.setUpdateBy(operName);
                   student.setId(sysStudent.getId());
                   this.updateSysStudent(student);
                   successNum++;
                   successMsg.append("<br/>" + successNum + "、账号 " +student.getUserName() + " 更新成功");
               }else{
                   failureNum++;
                   failureMsg.append("<br/>" + failureNum + "、账号 " + student.getUserName()+ " 已存在");
               }
            }
            catch (Exception e){
                failureNum++;
                String msg = "<br/>" + failureNum + "、账号 " + student.getUserName() + " 导入失败：";
                failureMsg.append(msg + e.getMessage());
                log.error(msg, e);
            }
        }
        if (failureNum > 0)
        {
            failureMsg.insert(0, "很抱歉，导入失败！共 " + failureNum + " 条数据格式不正确，错误如下：");
            throw new ServiceException(failureMsg.toString());
        }else{
            successMsg.insert(0, "恭喜您，数据已全部导入成功！共 " + successNum + " 条，数据如下：");
        }
        return successMsg.toString();
    }
}
